package com.example.thirdhomework

import android.annotation.SuppressLint
import android.content.Intent
import android.graphics.Bitmap
import android.os.Bundle
import android.provider.MediaStore
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

const val FIRST_MES = "FIRST MESSAGE"
const val SECOND_MES = "SECONDMENT"

class MainActivity : AppCompatActivity() {


    @SuppressLint("MissingInflatedId")
    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
//        正向传参
        val first: Button = findViewById(R.id.first)
        first.setOnClickListener {
            val intent = Intent(this, SecondActivity::class.java)
            val editText = findViewById<EditText>(R.id.editTextText)
            intent.putExtra(FIRST_MES,editText.text.toString())
            startActivity(intent)
        }
        //反向传参
        val second: Button = findViewById(R.id.second)
        second.setOnClickListener{
            val intent = Intent(this,ThirdActivity::class.java)
            startActivityForResult(intent,0)
        }

        //进入第三个界面
        val pic: Button = findViewById(R.id.third)
        pic.setOnClickListener{
            val intent = Intent(this, ForthActivity::class.java)
            startActivity(intent)

        }

//        进入第四个页面
        val time : Button = findViewById(R.id.forth)
        time.setOnClickListener{
            val intent = Intent(this, FifthActivity::class.java)
            startActivity(intent)
        }
    }
    @Deprecated("This method has been deprecated in favor of using the Activity Result API\n      which brings increased type safety via an {@link ActivityResultContract} and the prebuilt\n      contracts for common intents available in\n      {@link androidx.activity.result.contract.ActivityResultContracts}, provides hooks for\n      testing, and allow receiving results in separate, testable classes independent from your\n      activity. Use\n      {@link #registerForActivityResult(ActivityResultContract, ActivityResultCallback)}\n      with the appropriate {@link ActivityResultContract} and handling the result in the\n      {@link ActivityResultCallback#onActivityResult(Object) callback}.")
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 0){
            if(resultCode == 1){
                val text = findViewById<TextView>(R.id.textView)
                text.text = data?.getStringExtra(SECOND_MES)//?代表安全调用
            }
        }
    }
}